package president;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.Account;
import controllers.Accountability;
import controllers.BKCashAdvance;
import controllers.BKPayroll;
import controllers.Logs;
import controllers.Registered;

public class ViewFirst extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private Model descModel;
	private Model amountModel;
	private Model semesterModel;
	private Model schoolYearModel;
	private Model createdBy;
	private Model dateCreated;
	private Model ornumberModel = new Model();
	
	public ViewFirst(String id) throws IOException, SQLException
	{
		add(new BookmarkablePageLink("home", PresidentHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("first", ClearancePageFirst.class));
		
		final String formid = id;
		
		List<Accountability> acc = (List<Accountability>)sqlMap.queryForList("Accountability.getAccountability", formid);
		
		ListView eachRecord = new ListView("eachRecord", acc) {
			private static final long serialVersionUID = 1L;
			
			protected void populateItem(ListItem item) {
				final Accountability a = (Accountability) item.getModelObject();
				int idk = a.getAccountability_id();
				
				item.add(new CustomCheckBox("checkbox", new CustomCheckBoxModel(a.getAccountability_id())));
				item.add(new Label("desc", a.getAccountability_desc()));
				item.add(new Label("amount", Float.toString(a.getAmount())));
				item.add(new Label("semester", a.getSemester()));
				item.add(new Label("schoolYear", a.getSchoolyear()));
				item.add(new Label("createdBy", a.getUploader()));
				item.add(new Label("dateCreated", a.getDate_created()));
			}
		};
		
		Form form = new Form("viewForm");
		
		TextField ornumber = new TextField("ornumber", ornumberModel);
		ornumber.setRequired(true);
		
		form.add(ornumber);
		
		Registered reg = (Registered)sqlMap.queryForObject("Registered.getName", formid);
		String name = reg.getStudent_name();
		
		form.add(new Label("studentid", formid));
		form.add(new Label("name", name));
		
		final String uploadby = ((Sessions)getSession()).getDesc();
		
		Date today = new Date();
		Timestamp timestamp = new Timestamp(today.getTime());
		final String ts = timestamp.toString();
		
		form.add(new Button("clear"){
			private static final long serialVersionUID = 1L;

			public void onSubmit(){
			  try{
				 
				  if(selections.size()!=1)
				  {
						error("Please choose one item.");
				  }
				  else
				  {
						int acc_id = selections.get(0);
						
						String ornumber = (String)ornumberModel.getObject();
						
						Accountability acc = new Accountability ();
						acc.setOrNumber(ornumber);
						acc.setAccountability_id(acc_id);
						acc.setStatus("CLEARED");
						sqlMap.update("Accountability.updateStatus", acc);
						
						Logs log = new Logs();
						log.setLog_activity("Cleared: " + formid + " of accountability " + acc_id);
						log.setLog_officer(uploadby);
						log.setLog_time(ts);
						sqlMap.insert("Logs.insert", log);
											
						ClearancePageFirst page = new ClearancePageFirst();
						setResponsePage(page);
						
					}
				
			  }catch(SQLException e){
				  e.printStackTrace();
			  }catch(IOException e){
				  e.printStackTrace();
			  }
			}
		});
		
		form.add(new Button("cancel"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit(){
			  try{
		
				  ClearancePageFirst page = new ClearancePageFirst();
				setResponsePage(page);
			  }catch(SQLException e){
				  e.printStackTrace();
			  }catch(IOException e){
				  e.printStackTrace();
			  }
			}
		});
		
		
		form.add(eachRecord);
		add(form);
	}

	public class CustomCheckBox extends CheckBox{
		private static final long serialVersionUID = 1L;

		public CustomCheckBox(String id, IModel model) {
			super(id, model);
		}
		
		protected boolean wantOnSelectionChangedNotifications() {
			return true;
		}
	}

	private final List<Integer> selections = new ArrayList();
	
	public class CustomCheckBoxModel implements IModel, Serializable{
		private static final long serialVersionUID = 1L;
		private final int id;
		
		public CustomCheckBoxModel(int id) {
			super();
			this.id = id;
		}

		public IModel getNestedModel() {
			return null;
		}

		public Object getObject(){
			return isSelected();
		}
		
		private Boolean isSelected() {
			if (selections.contains(id))
				return true;
			else
				return false;
		}
			
		public void setObject(Object object){
			boolean selected = ((Boolean) object).booleanValue();
			boolean previouslySelected = isSelected();
			if (selected){
				if (!previouslySelected) {
					selections.add(id);
				}
			} else {
				if (previouslySelected) {
					selections.remove((Integer)id);
				}
			}
		}
		public void detach() {}
	}
}
